<%-- 
    Document   : content
    Created on : Apr 24, 2012, 4:41:55 PM
    Author     : Asus
--%>



<%@page import="java.util.Calendar"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="model.TagModel"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@include file="/connect.jspf"%>
<%@include file="/header.jsp"%>

<%@page import="model.PostModel, model.TagModel, model.UserModel, java.util.Date, util.Utility, model.CommentModel" %>

<%
    PostModel postModel = new PostModel(con);
    TagModel tagModel = new TagModel(con);
    CommentModel commentModel = new CommentModel(con);  
    
    Date pageLoadTime = new Date();
    String pageLoadTimeString = pageLoadTime.toString();
    
    int logged_in_user_id = -1;
    if(session.getAttribute("userid") != null) {
        logged_in_user_id = Integer.parseInt(session.getAttribute("userid").toString());
    }
    
    if(request.getParameter("id")==null) {
        out.println("invalid link");
        return;
    }
    
    int post_id = Integer.parseInt(request.getParameter("id").toString());    
    //int post_id = 130;
    ResultSet post_info = postModel.getPost(post_id);
    post_info.next();
    String poster_name = post_info.getString("username");
    int poster_id = post_info.getInt("poster_id");
    
//FIXME : masih belommmm, harusnya post_datetime berisi tanggal post itu diterbitkan       
    Calendar post_datetime = new GregorianCalendar();
    Calendar now = new GregorianCalendar();
    String post_diff_datetime_string = Utility.getDeltaTimeString(post_datetime, now);
    
    String post_title = post_info.getString("title");
    int post_comment_count = post_info.getInt("post_comment_count");
    int post_like_count = post_info.getInt("post_like_count");
    String post_content = "";
    
    String link;
    String description = "";    
    String tube_id;
    
    if(post_info.getString("post_type").equals("link")) {
        String[] temp = post_info.getString("content").split("\n");
        link = temp[0];
        if(temp.length>1) {
            description = temp[1];
        }
        post_content = "<h2>" + link + "</h2>" + " " + "deskripsi : " + description;
    }else if(post_info.getString("post_type").equals("image")) {
        post_content = "<img class='fullimage' src='upload/" + post_info.getString("content") + "' />";
    }else if(post_info.getString("post_type").equals("video")) {
        tube_id = post_info.getString("content").substring(31, post_info.getString("content").length());
        post_content = "<div id='video'>" +
			"<object type='application/x-shockwave-flash' style='width:560px; height:315px;' data='http://www.youtube.com/v/" + tube_id + "?version=3'>" +
                        "<param name='movie' value='http://www.youtube.com/v/'" + tube_id + "'?version=3' />" +
                        "<param name='allowFullScreen' value='true' />"+
			"<param name='allowscriptaccess' value='always' />"+
                        "</object>"+
			"</div>";
    }
%>

<!-- Body -->
<div id="bodycontent">
    <div class="contentcontainer">

        <div class="title-text">
            <h1><%= post_title %></h1>
        </div>

        <div class="contentbox">		  
            <div id="content-desc">
                <p><%= post_content %></p>
                <p><small><i>Uploaded by <a href="profile.jsp?id=<%= poster_id %>"><%= poster_name %></a> <%= post_diff_datetime_string %> </i></small></p>
                <p><small>
                        <% 
                        out.println("tags : ");
                        ResultSet post_tags_res = tagModel.getTags(post_id+"");				
                        if(post_tags_res.next())
                            out.println(post_tags_res.getString("type"));
                        while(post_tags_res.next()) {
                            out.println(", " + post_tags_res.getString("type"));    
                        }                                                			
                        %>
                    </small>
                </p>
                <p><small>
                        <%= "<span id='comment-count'>" + post_comment_count + "</span> peoples commented" %>
                    </small></p>
            </div>		  
            <div id="content-like">
                <%

                out.println("<a onclick=doLikeOp(\"like\\@" + post_id + "\",\"id\")><img src=like.png width=20 height=20 alt=like></a>");
                out.println("<a onclick=doLikeOp(\"dislike\\@" + post_id + "\",\"id\")><img src=dislike.png width=20 height=20 alt=dislike></a>");
                //$post_model->printLike($post_like_count,$logged_in_user_id,$_GET["id"]);
                out.println(postModel.getLikeWithClassString(post_like_count, logged_in_user_id, post_id));
                %>
            </div>			  
            <% if(session.getAttribute("userid")!=null) { %>
            <h4>Tambahkan Komentar</h4>			  
            <div id="content-inp-comment">
                Komentar
                <br/>				
                <textarea name="content" id="content-input-comment" rows="2" cols="20"></textarea>
                <span id="comment-content-error" class='form-error'></span>
            </div>
            <div id="content-add-comment">
                <input type="submit" value="Tambahkan komentar" id="content-post-comment" onclick="addComment(document.getElementById('content-input-comment').value, <%= post_id %>, <%= logged_in_user_id %>);" />
            </div>
            <input type="hidden" name="submitted" value="1"> 		
            <% } %>
            <div id="content-comments">
                <%	
                ResultSet comment_res = commentModel.getAllComment(post_id);	                
                while(comment_res.next()) {
                    
                //FIXME :                
                //$comment_datetime = new DateTime($row['datetime'], new DateTimeZone('Asia/Jakarta'));	
                //$now = new DateTime(null, new DateTimeZone('Asia/Jakarta'));	
                //$comment_diff_datetime_string = Utility::getDeltaTimeString($comment_datetime, $now);
                String comment_diff_datetime_string = "";
                
                out.println("<div class='content-comment' id='comment" + comment_res.getString("comment_id") + "'>");
                out.println("<img class='avatar' src='avatar/" + comment_res.getString("avatar") + "' />");
                    out.println("<h4>" + comment_res.getString("username") + "</h4>");
                    out.println("<p>" + comment_res.getString("content") + "</p>");						
                    out.println("<span name='comment-date'><small>" + comment_diff_datetime_string + "</small></span>");						
                    if(comment_res.getString("username_id").equals(logged_in_user_id+""))
                        out.println("<input type='button' onclick='delComment(" + comment_res.getString("comment_id") + ");' value='delete' />");
                    out.println("</div>");
                }                				
                %>						 							
            </div>
        </div>

    </div>
</div>	

<input type="hidden" id="pageLoadTime" value="" />

<script type="text/javascript">
    document.getElementById("pageLoadTime").value = new Date();		
</script>